#!/bin/bash

#Description:启动dubbo服务脚本
#sh restart.sh

export PATH=$JAVA_HOME/bin:$PATH

#通过读取conf/appinfo.txt获取
MAIN_CLASS=""
# 必填项, 如果不存在直接退出
# 20个字符以内
appname=""
# pinpoint agentid自动根据内网IP获取
agentid=""

#-------------------------------------------------------------------
#    Mb Bootstrap Script
#-------------------------------------------------------------------
function main()
{
        # find Mb home.
        CURR_DIR=`pwd`
        #cd `dirname "$0"`/..
        RESV_HOME=`pwd`
        #cd $CURR_DIR

        if [ -z "$RESV_HOME" ] ; then
                echo
                echo Must set RESV_HOME
                echo
                exit 1
        fi
        get_app_info
        pid=`ps -ef | grep -w "$MAIN_CLASS" |grep -v "grep"| awk '{print $2}'`
        echo "exists process pid $pid "
        if [ $pid ];
                then
                echo "kill the $pid process"
                kill -9 $pid
                echo "stop success"
        else
            echo "start......"
        fi

        execute
}

function get_app_info()
{
        #get inner ip
        eth_out=eth0
        eth_inner=eth1
        issuse=`cat /etc/issue|grep suse`
        if [ -n "$issue" ];then
                issuse=1
        else
                issuse=0
        fi
        if [ ${issuse} -eq 1 ];then
                eth_out=em1
                eth_inner=em2
        fi
        ip_outer=`/sbin/ifconfig ${eth_out} 2>/dev/null |grep "inet addr:"|awk -F ":" '{ print $2 }'|awk '{ print $1 }'`
        ip_inner=`/sbin/ifconfig ${eth_inner} 2>/dev/null |grep "inet addr:"|awk -F ":" '{ print $2 }'|awk '{ print $1 }'`
        if [ "${ip_inner}x" = "x" ];then
                #ip_inner=`/sbin/ip route|grep 'eth1'|grep 'scope link  src'|awk '{print $9}'|head -n 1`
                ip_inner=`/sbin/ip route|egrep 'src 172\.|src 10\.'|awk '{print $9}'|head -n 1`
        fi

        agentid=`echo ${ip_inner//./-}`
        if [ "${agentid}x" = "x" ];then
                echo "get agentid failed, please configure agentid in file catalina.sh"
        fi

        if [ ! -f ${RESV_HOME}/conf/appinfo.txt ];then
            echo "${RESV_HOME}/conf/appinfo.txt not found, please config it"
            exit 1
        fi
        appname=`cat ${RESV_HOME}/conf/appinfo.txt |grep appname|awk -F'=' '{print $2}'`
        # !!!please configure appname
        if [ "${appname}x" = "x" ];then
            echo "appname must not to be null, please configure appname in file ${RESV_HOME}/conf/appinfo.txt"
            exit
        fi
        len=${#appname}
        if [ ${len} -ge 24  ];then
            echo "appname can only contain [a-zA-Z0-9], '.', '-', '_'. maxLength:24 current value:${appname}"
            exit
        fi
        MAIN_CLASS=`cat ${RESV_HOME}/conf/appinfo.txt |grep main_class|awk -F'=' '{print $2}'`
        # !!!please configure appname
        if [ "${MAIN_CLASS}x" = "x" ];then
            echo "MAIN_CLASS must not to be null, please configure main_class in file ${RESV_HOME}/conf/appinfo.txt"
            exit
        fi
}

# 执行启动命令
function execute()
{
        mkdir -p /data/logs/zhenai-crm-center-provider
        chown nobody:nobody /data/logs/zhenai-crm-center-provider
        for i in $RESV_HOME/lib/*.jar; do
                 CLASSPATH=$i:$CLASSPATH;
        done
        echo RESV_HOME=$RESV_HOME
        CLASSPATH=$RESV_HOME/conf:$RESV_HOME/zhenai-crm-center-provider.jar

        DEBUG_INFO=" -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n"
        DEBUG=""

        DEFAULT_OPTS="-Djava.ext.dirs=${RESV_HOME}:${RESV_HOME}/lib"
        DEFAULT_OPTS="$DEFAULT_OPTS -Ddubbo.application.logger=slf4j -server -Xms2000m -Xmx2000m -XX:PermSize=256m "
        DEFAULT_OPTS="$DEFAULT_OPTS -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=60"
        DEFAULT_OPTS="$DEFAULT_OPTS -DMB.home=\"$RESV_HOME\""

        # 添加pinpoint数据上报
        if [ -f /data/opt/pp-agent/pinpoint-bootstrap-1.6.0.jar ];then
            DEFAULT_OPTS="$DEFAULT_OPTS -javaagent:/data/opt/pp-agent/pinpoint-bootstrap-1.6.0.jar"
            DEFAULT_OPTS="$DEFAULT_OPTS -Dpinpoint.agentId=${agentid}"
            DEFAULT_OPTS="$DEFAULT_OPTS -Dpinpoint.applicationName=${appname}"
        fi

        echo java $DEBUG $DEFAULT_OPTS  -classpath $CLASSPATH  $MAIN_CLASS
        nohup java $DEBUG $DEFAULT_OPTS  -classpath $CLASSPATH  $MAIN_CLASS >> /data/logs/zhenai-crm-center-provider/stdout.log  2>&1 &
        sleep 5
        echo "start success"
}

main
